<template>
  <div id="customer-passengers">
    <div class="card bg-transparent">
      <div class="card-header my-0 py-0">
        <nav aria-label="breadcrumb" role="navigation">
          <ol class="breadcrumb pb-0">
            <li class="breadcrumb-item">
              <router-link to="/customers">企业客户</router-link>
            </li>
            <li class="breadcrumb-item active" aria-current="page">员工信息</li>

            <li class="breadcrumb-item ms-auto">
              <router-link to="/customer/departments">部门信息</router-link>
            </li>
          </ol>
        </nav>
      </div>
      <div class="card-body my-1 py-1">
        <form class="row">
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              placeholder="姓名"
              name="sc.name"
              v-model.trim="searchName"
            />
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              placeholder="手机号"
              v-model.trim="mobile"
            />
          </div>
          <div class="col-2">
            <my-select-customer
              :customerId.sync="customerId"
              :status="1"
              sizing="sm"
            ></my-select-customer>
          </div>
          <div class="col-1">
            <select
              class="form-select"
              v-model.number="sc.pageSize"
            >
              <option value="10">10</option>
              <option value="20">20</option>
              <option value="50">50</option>
              <option value="100">100</option>
              <option value="200">200</option>
              <option value="500">500</option>
            </select>
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-primary ms-1"
              @click="search()"
            >
              查找
            </button>
            <button
              type="button"
              class="btn btn-secondary btn-sm ms-2"
              title="清空查找条件"
              @click="reset()"
            >
              重置
            </button>

            <router-link :to="`/passenger`" class="btn btn-success ms-2 float-end"
              >新建</router-link
            >

            <router-link
              to="/passenger-key-customer-policy"
              class="btn btn-info ms-2  float-end"
              >关联机票私有政策</router-link
            >

            <button
              type="button"
              class="btn btn-primary ms-2  float-end"
              @click.stop="importExcel()"
            >
              导入Excel
            </button>

            <a
              class="btn btn-info ms-2  float-end"
              @click.stop="download(0)"
              href="javascript:void(0)"
              >下载csv</a
            >
            <a
              class="btn btn-info ms-2 float-end"
              @click.stop="download(1)"
              href="javascript:void(0)"
              >下载Excel</a
            >
          </div>
        </form>
      </div>
    </div>

    <passenger-list :dataList="dataList"></passenger-list>

    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <modal-excel-upload ref="modalUploadExcel">
      用Excel导入员工信息
      <template v-slot:excelFormatDesc
        >Excel格式说明: 票号，销售日期(可空)</template
      >
    </modal-excel-upload>
  </div>
</template>

<script>
import { APP_FLIGHT_PATH } from "@/common/common.js";
import { searchPassengers, uploadPassengers } from "@/api/user";
import MyPagination from "@/components/my-pagination.vue";
import PassengerList from "@/components/list/passenger-list.vue";
import MySelectCustomer from "@/components/my-select2-customer.vue";
import ModalExcelUpload from "@/components/modal/my-modal-excel-upload-passengers.vue";

export default {
  name: "passengers",
  components: {
    MyPagination,
    PassengerList,
    MySelectCustomer,
    ModalExcelUpload,
  },
  data() {
    return {
      dataList: [],

      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      searchName: "",
      mobile: "",
      customerId: -1,
    };
  },
  mounted: function () {
    // this.search()
    if (window.matchMedia("(max-width: 576px)").matches) {
      // console.log(this.$router)
      this.$router.replace("/h5/passengers");
    } else {
      this.search();
    }
  },
  activated: function () {
    if (window.matchMedia("(max-width: 576px)").matches) {
      console.log(this.$router);
      this.$router.replace("/h5/passengers");
    } else {
      this.search();
    }
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", {
        loading: false,
      });
    },
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.name": this.searchName,
        "sc.mobile": this.mobile,
        "sc.customerId": this.customerId,
      };
      // this.showLoading('查询中...')
      searchPassengers(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
        // this.hideLoading()
      });
    },
    reset: function () {
      this.searchName = "";
      this.mobile = "";
      this.customerId = -1;
      this.sc.pageNo = 1;
      this.search();
    },
    download: function (fileFormat) {
      const url =
        APP_FLIGHT_PATH + "/admin/passengers/download?fileFormat=" + fileFormat + "&customerId=" + this.customerId;
      window.open(url);
    },
    importExcel: function () {
      this.$refs.modalUploadExcel
        .modal()
        .then((formData) => {
          console.log("processing....");
          this.showLoading("处理中......");
          uploadPassengers(
            formData.data,
            (v) => {
              if (v.status === "OK") {
                this.showErrMsg("操作成功");
              } else {
                this.showErrMsg("操作失败:" + v.errmsg, "danger");
              }
            },
            () => {
              this.hideLoading();
            }
          );
        })
        .catch((ex) => {});
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
